package com.eselect.config;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQMessageConfiguration {
    /**
     * 配置RabbitMQ的消息序列化器
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 配置RabbitMQ的消费者重试极限后的处理方式
     * RepublishMessageRecoverer: 重试极限后 将消息投递到指定交换机进行特殊处理
     */
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        String errorExchange = "error.exchange";
        String errorRoutingKey = "error.routing.key";
        return new RepublishMessageRecoverer(rabbitTemplate,errorExchange,errorRoutingKey);
    }
}
